const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab:'0',
    btnText:'直接雇佣',
    talentWorksIndex:0,
    evaluationTypeIndex:0,
    id:'',
    talentDetail:'',
    pageNum:1,
    commentList:'',
    commentListObj:'',
    serviceList:'',
    workList:[],
    successList:'',
    shopCollectStatus:'',
    shopType:1,
    uid:'',
    onFoldShow:false,
    onFold:false,
    setShow:false,
    serviceLoad:false,
    evaluationLoadBool:false,
    evaluationLoad:false,
    workListLoad:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideHomeButton()
    if( options.id){
      this.setData({
        id: options.id,
        shopType: options.shopType,
      })
    }
    this.setData({
      uid:wx.getStorageSync('uid')
    })
    if(options.title){
      wx.setNavigationBarTitle({
        title:'我的店铺',
      })
      this.setData({
        setShow:true,
      })
      this.myShopInfo();


    }else {
      wx.setNavigationBarTitle({
        title:'店铺详情',
      })
      // 详情
      this.talentDetail();
    }

    var { shopType } = this.data;
    if(shopType == 2){
      this.setData({
        activeTab: '2',
      })
      // 评价列表
      this.commentList();
    }

  },

  // 我的店铺信息设置后
  myShopChange:function(){
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/myShop',
      data: {
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          _this.setData({
            id:res.data.data.id,
            talentDetail:res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },



  // 我的店铺信息
  myShopInfo:function(){
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/myShop',
      data: {
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          _this.setData({
            id:res.data.data.id,
            talentDetail:res.data.data
          })
          _this.listDetail()
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  // 详情
  talentDetail:function(){
    var { id, shopType, uid } = this.data;
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    if (shopType != 2){//店铺
      wx.request({
        url: app.globalData.baseUrl + 'shop/shopDetail',
        data: {
          shop_id: id
        },
        success(res) {
          wx.hideLoading()
          if (res.data.code == 1000) {
            _this.setData({
              talentDetail: res.data.data
            })
            if(res.data.data.uid===uid){
              _this.setData({
                setShow:true,
              })
            }
            _this.listDetail()
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }else{//空间
      wx.request({
        url: app.globalData.baseUrl + 'shop/userDetail',
        data: {
          uid: id
        },
        success(res) {
          wx.hideLoading()
          console.log(res)
          if (res.data.code == 1000) {
            _this.setData({
              talentDetail: res.data.data
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },

  /**
   * 列表加载
   */
  listDetail(){
    // 作品列表
    this.workList();
    // 服务列表
    this.serviceList();
    // 案例列表
    this.successList();
    // 评价列表
    this.commentList();
  },


  onChange(e){
    this.setData({
      talentWorksIndex:e.detail.name
    })
    if(e.detail.index==4){
      this.checkFold()
    }
  },


  collection(){
    if(this.data.shopCollectStatus.status){
      this.delCollectBtn()
    }else {
      this.collectBtn()
    }
  },



  //文字收起打开
  checkFold(){
    let query = wx.createSelectorQuery();
    query.select('.info-text-box').boundingClientRect()
    query.select('.info-text').boundingClientRect()
    query.exec(res=>{
      const [ fixedElement, emptyElement ] = res;
      let data=emptyElement.height>fixedElement.height
      this.setData({onFoldShow:data})
    })
  },

  changeFold(){
    this.setData({onFold:!this.data.onFold})
  },

  setUp(){
    let {talentDetail} = this.data;
    let talentDetailJson = JSON.stringify(talentDetail)
    wx.navigateTo({
      url: '/userPage/pages/storeSettings/storeSettings?shop_id=' + talentDetail.id + '&myShopInfoObj=' + talentDetailJson,
    })
  },


  // 评级类别
  evaluationTypeBtn:function(e){
    var { index } = e.target.dataset
    this.setData({
      evaluationTypeIndex:index,
      pageNum:1,
      commentListObj:''
    })
    this.commentList();
  },

  // 店铺评价列表
  commentList:function(){
    var { id, pageNum, evaluationTypeIndex, commentListObj , shopType } = this.data;
    var _this = this;
    this.setData({
      evaluationLoadBool:true,
      evaluationLoad:true,
    })
    var url = '';
    var data = {};
    if(shopType != 2){
      url = app.globalData.baseUrl + 'shop/commentList',
      data = {
        shop_id: id,
        page: pageNum,
        type: evaluationTypeIndex
      };
    }else{
      url = app.globalData.baseUrl + 'shop/taskCommentList',
      data = {
        uid: id,
        page: pageNum,
        type: evaluationTypeIndex
      };
    }
    wx.request({
      url: url,
      data: data,
      success(res) {

        _this.setData({
          evaluationLoadBool:false,
          evaluationLoad:false,
        })
        console.log(res)
        if (res.data.code == 1000) {
          if (pageNum != 1) {
            if (res.data.data.data == '') {
              wx.showToast({
                title: '暂无更多',
                icon: 'none',
                duration: 2000
              })
              return false;
            }
          }
          console.log(res.data,'评论列表-----------')
          _this.setData({
            commentList: res.data.data,
            commentListObj: [ ...commentListObj,...res.data.data.data.data]
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 收藏状态
  shopCollectStatus:function(){
    if (!app.globalData.token){
      return false
    }
    var {id} = this.data;
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'shop/collectStatus',
      data: {
        shop_id: id,
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            shopCollectStatus: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  // 联系
  contactBtn: function () {

    var { talentDetail } = this.data;
    console.log(talentDetail.mobile);
    if (talentDetail.mobile == null){
      wx.showToast({
        title: '该服务商还未设置联系方式',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.makePhoneCall({
      phoneNumber: talentDetail.mobile
    })
   /* wx.makePhoneCall({
      phoneNumber: '13465423645'
    })*/
  },
  // 收藏
  collectBtn: function () {
    var { id } = this.data
    var _this = this;
    if (!app.globalData.token) {
      wx.showModal({
        title: '提示',
        content: '您未登录，是否去登陆？',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/userPage/pages/login/login',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'shop/collect',
      data: {
        shop_id: id,
        token: app.globalData.token
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            _this.shopCollectStatus();
          }, 1000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 取消收藏
  delCollectBtn: function (e) {
    var { id } = this.data
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'shop/cancelCollect',
      data: {
        shop_id: id,
        token: app.globalData.token
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '取消成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            _this.shopCollectStatus();
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 服务列表
  serviceList:function(){
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      serviceLoad:false,
    })
    var { id, pageNum, serviceList } = this.data;
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'shop/serviceList',
      data: {
        shop_id: id,
        page: pageNum
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          if (pageNum != 1) {
            if (res.data.data.data == '') {
              wx.showToast({
                title: '暂无更多',
                icon: 'none',
                duration: 2000
              })
              return false;
            }
          }
          _this.setData({
            serviceList: [...serviceList, ...res.data.data.data]
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
        _this.setData({
          serviceLoad:_this.data.serviceList.length===0
        })
      }
    })
  },
  // 作品列表
  workList:function(){
    var { id, pageNum, workList } = this.data;
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      workListLoad:false
    })
    wx.request({
      url: app.globalData.baseUrl + 'shop/workList',
      data: {
        shop_id: id,
        page: pageNum
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          if (pageNum != 1){
            if (res.data.data.data == '') {
              wx.showToast({
                title: '暂无更多',
                icon: 'none',
                duration: 2000
              })
              return false;
            }
          }
          _this.setData({
            workList: [...workList, ...res.data.data.data]
          })

        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
        _this.setData({
          workListLoad:_this.data.workList.length===0
        })
      }
    })
  },
  // 案例列表
  successList:function(){
    var { id, pageNum, successList } = this.data;
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'shop/successList',
      data: {
        shop_id: id,
        page: pageNum
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          if (pageNum != 1) {
            if (res.data.data.data == '') {
              wx.showToast({
                title: '暂无更多',
                icon: 'none',
                duration: 2000
              })
              return false;
            }
          }
          _this.setData({
            successList: [...successList, ...res.data.data.data]
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 雇佣
  hireBtn:function(){
    var { talentDetail,id } = this.data;
    var _this = this;
    if (!app.globalData.token){
      wx.showToast({
        title: '您还未登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'shop/isEmploy',
      data: {
        token: app.globalData.token,
        id: id
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          wx.navigateTo({
            url: '/pages/hireForm/hireForm?userName=' + talentDetail.username + '&uid=' + talentDetail.uid,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 作品或服务详情
  shopList: function (e) {
    const { id ,type} = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/taskPage/pages/worksDetail/worksDetail?id=' + id + '&type=' + type,
    })
  },
  // 案例
  caseDetailBtn: function (e) {
    var { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/taskPage/pages/caseDetail/caseDetail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton();
    // 店铺收藏
    this.shopCollectStatus();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var { talentWorksIndex,pageNum } = this.data;
    pageNum ++;
    this.setData({
      pageNum: pageNum
    })
    if (talentWorksIndex == 0){
      // 服务列表
      this.serviceList();

    } else if (talentWorksIndex == 1){

      // 作品列表
      this.workList();
    } else if (talentWorksIndex == 2){
      // 评价列表
      this.commentList();

    } else if (talentWorksIndex == 3) {
      // 案例列表
      this.successList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var { talentDetail, shopType,id } = this.data
    var title = '';
    if (shopType != 2){
      title = talentDetail.shop_name
    }else{
      title = talentDetail.name
    }
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: title,
      path: '/taskPage/pages/talentDetail/talentDetail?id=' + id + '&shopType=' + shopType
    }
  }
})
